﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WEALTH.Manager.Customer
{
    public partial class LeftMenuList : BasePage
    {
        private ExcuteDao LeftMenuManage = new ExcuteDao();
        protected void Page_Load(object sender, EventArgs e)
        {             
            if (Request.QueryString["UrlType"] != null && Request.QueryString["UrlType"].Equals("menu"))
            {
                Session["SortExpression_LeftMenuList"] = null;
                Session["SortDirection_LeftMenuList"] = null;
                Session["txtLeftMenuName"] = null;
                Session["txtLeftMenuType"] = null;
            }

            if (Session["UrlType"] != null)
            {
                if (Session["UrlType"].Equals("EditLeftMenu"))
                {
                    setSessionData();
                    if (Session["LeftMenuListIndex"] != null && string.IsNullOrEmpty(Request.QueryString["pageIndex"]))
                    {
                        gvLeftMenu.PageIndex = (int)Session["LeftMenuListIndex"];
                    }

                    Session["UrlType"] = null;
                }
            }
            if (!string.IsNullOrEmpty(Request.QueryString["pageIndex"]))
            {
                string pageIndex = Request.QueryString["pageIndex"];
                int index = Convert.ToInt32(pageIndex);
                gvLeftMenu.PageIndex = index;

                Session["LeftMenuListIndex"] = index;

                setSessionData();
            }
            //设置分页状态
            if (Session["SortExpression_LeftMenuList"] != null)
            {
                this.gvLeftMenu.Attributes["SortExpression_LeftMenuList"] = Session["SortExpression_LeftMenuList"].ToString();
                this.gvLeftMenu.Attributes["SortDirection_LeftMenuList"] = Session["SortDirection_LeftMenuList"].ToString();
                setSortColumnStyle(this.gvLeftMenu, Session["SortExpression_LeftMenuList"].ToString(), Session["SortDirection_LeftMenuList"].ToString());
            }
            //setSortColumnStyle(gvLeftMenu, this.gvLeftMenu.Attributes["SortExpression_LeftMenuList"].ToString(),
            //     this.gvLeftMenu.Attributes["SortDirection_LeftMenuList"].ToString());
            if (!IsPostBack)
            {
                ViewState["LeftMenuAll"] = LeftMenuManage.Get_LeftMenuALL(
                                                SpecialCharHandler.HtmlEncode(txtLeftMenuName.Text),
                                                SpecialCharHandler.HtmlEncode(txtLeftMenuType.Text));

                //初次加载清空分页状态
                //if (string.IsNullOrEmpty(Request.QueryString["pageIndex"]) && Session["UrlType"] == null)
                //{
                //    Session["SortExpression_LeftMenuList"] = null;
                //    Session["SortDirection_LeftMenuList"] = null;
                //}
                SearchLeftMenuBindData();
            }
        }
      
        private void setSessionData()
        {
            //检索条件记录
            if (Session["txtLeftMenuName"] != null)
            {
                txtLeftMenuName.Text = SpecialCharHandler.HtmlDiscode(Session["txtLeftMenuName"].ToString());
            }           
            if (Session["txtLeftMenuType"] != null)
            {
                txtLeftMenuType.Text = SpecialCharHandler.HtmlDiscode(Session["txtLeftMenuType"].ToString());

            }           

            if (Session["LeftMenuListIndex"] != null)
            {
                gvLeftMenu.PageIndex = (int)Session["LeftMenuListIndex"];
            }
        }
        #region 前台菜单列表事件

        /// <summary>
        /// 前台菜单列表数据加载时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvLeftMenu_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("id", e.Row.RowIndex.ToString());
                e.Row.Attributes.Add("onKeyDown", "SelectRow();");
                e.Row.Attributes.Add("onClick", "MarkRow(" + e.Row.RowIndex.ToString() + ");");
                ((HyperLink)e.Row.Cells[0].Controls[0]).NavigateUrl = "../Customer/EditLeftMenu.aspx?LeftMenuID="
                    + SpecialCharHandler.HtmlDiscode(((HyperLink)e.Row.Cells[0].Controls[0]).Text).Replace("&", "amp;");

                string strLeftMenuName = SpecialCharHandler.HtmlDiscode(e.Row.Cells[1].Text);
                e.Row.Cells[1].ToolTip = strLeftMenuName;
                if (strLeftMenuName.Length > 15)
                {
                    strLeftMenuName = strLeftMenuName.Substring(0, 15) + "...";
                }
                e.Row.Cells[1].Text = strLeftMenuName;

                string strLeftMenuType = SpecialCharHandler.HtmlDiscode(e.Row.Cells[2].Text);
                e.Row.Cells[2].ToolTip = strLeftMenuType;
                if (strLeftMenuType.Length > 15)
                {
                    strLeftMenuType = strLeftMenuType.Substring(0, 15) + "...";
                }
                e.Row.Cells[2].Text = strLeftMenuType;               

            }
        }
        #endregion

        /// <summary>
        /// 搜索按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            ViewState.Clear();
            ViewState["LeftMenuAll"] = LeftMenuManage.Get_LeftMenuALL(
                                                SpecialCharHandler.HtmlEncode(txtLeftMenuName.Text),
                                                SpecialCharHandler.HtmlEncode(txtLeftMenuType.Text));
            //清空分页状态
            Session["SortExpression_LeftMenuList"] = null;
            Session["SortDirection_LeftMenuList"] = null;

            //检索条件记录
            Session["txtLeftMenuName"] = SpecialCharHandler.HtmlEncode(txtLeftMenuName.Text);
            Session["txtLeftMenuType"] = SpecialCharHandler.HtmlEncode(txtLeftMenuType.Text);          

            SearchLeftMenuBindData();

        }

        /// <summary>
        /// 选择页面功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvLeftMenu_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvLeftMenu.PageIndex = e.NewPageIndex;
            pageIndex = e.NewPageIndex;
            Session["LeftMenuListIndex"] = pageIndex;
            SearchLeftMenuBindData();
        }

        /// <summary>
        /// GridView排序事件
        /// </summary>
        protected void gvLeftMenu_Sorting(object sender, GridViewSortEventArgs e)
        {
            // 从事件参数获取排序数据列
            string SortExpression_LeftMenuList = e.SortExpression.ToString();
            //// 假定为排序方向为“顺序”
            string SortDirection_LeftMenuList = "ASC";
            // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
            if (SortExpression_LeftMenuList == this.gvLeftMenu.Attributes["SortExpression_LeftMenuList"])
            {
                //获得下一次的排序状态
                SortDirection_LeftMenuList = (this.gvLeftMenu.Attributes["SortDirection_LeftMenuList"].ToString() == SortDirection_LeftMenuList ? "DESC" : "ASC");
            }
            // 重新设定GridView排序数据列及排序方向
            this.gvLeftMenu.Attributes["SortExpression_LeftMenuList"] = SortExpression_LeftMenuList;
            this.gvLeftMenu.Attributes["SortDirection_LeftMenuList"] = SortDirection_LeftMenuList;
            //记录分页状态
            Session["SortExpression_LeftMenuList"] = SortExpression_LeftMenuList;
            Session["SortDirection_LeftMenuList"] = SortDirection_LeftMenuList;

            setSortColumnStyle(this.gvLeftMenu, SortExpression_LeftMenuList, SortDirection_LeftMenuList);
            this.SearchLeftMenuBindData();
        }

        /// <summary>
        /// 绑定到GridView
        /// </summary>
        private void SearchLeftMenuBindData()
        {
            // 获取GridView排序数据列及排序方向
            string SortExpression_LeftMenuList = this.gvLeftMenu.Attributes["SortExpression_LeftMenuList"];
            string SortDirection_LeftMenuList = this.gvLeftMenu.Attributes["SortDirection_LeftMenuList"];
            // 调用业务数据获取方法
            DataTable dtBind = (DataTable)ViewState["LeftMenuAll"];

            //页面数据总数
            recordCount = dtBind.Rows.Count;

            // 根据GridView排序数据列及排序方向设置显示的默认数据视图
            if ((!string.IsNullOrEmpty(SortExpression_LeftMenuList)) && (!string.IsNullOrEmpty(SortDirection_LeftMenuList)))
            {
                dtBind.DefaultView.Sort = string.Format("{0} {1}", SortExpression_LeftMenuList, SortDirection_LeftMenuList);
            }
            // GridView绑定并显示数据
            this.gvLeftMenu.DataSource = dtBind;
            this.gvLeftMenu.DataBind();

            pageCount = gvLeftMenu.PageCount;
            pageIndex = gvLeftMenu.PageIndex;

        }
        protected void gvLeftMenu_DataBound(object sender, EventArgs e)
        {
            if (Session["Edit_LeftMenuID"] != null)
            {
                foreach (GridViewRow gvr in gvLeftMenu.Rows)
                {
                    if (((HyperLink)gvr.Cells[0].Controls[0]).Text.Equals(
                        SpecialCharHandler.HtmlDiscode(Session["Edit_LeftMenuID"].ToString())))
                    {
                        //gvLeftMenu.SelectedIndex = gvr.RowIndex;
                        //gvLeftMenu.Rows[gvr.RowIndex].BackColor = System.Drawing.Color.FromName("#D8EBF0");
                        ClientScript.RegisterStartupScript(GetType(), "SelectRow", "<script>MarkRow(" + gvr.RowIndex + "); </script>");

                    }
                }
                Session["Edit_LeftMenuID"] = null;
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            int isok = 0;

            bool ischecked = false;
            for (int i = 0; i <= gvLeftMenu.Rows.Count - 1; i++)
            {
                CheckBox CheckBox = (CheckBox)gvLeftMenu.Rows[i].FindControl("chkdelete");
                if (CheckBox.Checked == true)
                {
                    string LeftMenuid = ((HyperLink)gvLeftMenu.Rows[i].Cells[0].Controls[0]).Text;
                    isok = LeftMenuManage.Delete_LeftMenu(Convert.ToInt32(LeftMenuid));
                    ischecked = true;
                }

            }
            if (ischecked)
            {
                ischecked = false;
                if (isok == 1)
                {
                    ShowMsg("删除前台菜单成功！", 0);
                    ViewState["LeftMenuAll"] = LeftMenuManage.Get_LeftMenuALL(
                                                SpecialCharHandler.HtmlEncode(txtLeftMenuName.Text),
                                                SpecialCharHandler.HtmlEncode(txtLeftMenuType.Text));
                    SearchLeftMenuBindData();
                }
                else
                {
                    ShowMsg("删除前台菜单失败！", 1);
                }
            }
        }
    }
}